package utils;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.GridBagLayout;
import javax.swing.JSplitPane;
import java.awt.GridBagConstraints;
import javax.swing.JTextField;
import java.awt.Insets;

import javax.swing.JFileChooser;
import javax.swing.JTextPane;
import javax.swing.JScrollPane;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JComboBox;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.File;

public class XMLGenerateGui extends JFrame {

	private JPanel contentPane;
	private JTextField tbannfile;
	private JTextField tboutputfile;
	private File inputFile;
	private File outputDir;
	private File inputTextFile;
	private String filetype;
	private JComboBox comboBox; 
	private JTextPane consolePane;
	private JTextField tbtextfile;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					XMLGenerateGui frame = new XMLGenerateGui();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public XMLGenerateGui() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 600, 400);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		GridBagLayout gbl_contentPane = new GridBagLayout();
		gbl_contentPane.columnWidths = new int[]{0, 0};
		gbl_contentPane.rowHeights = new int[]{0, 0};
		gbl_contentPane.columnWeights = new double[]{1.0, Double.MIN_VALUE};
		gbl_contentPane.rowWeights = new double[]{1.0, Double.MIN_VALUE};
		contentPane.setLayout(gbl_contentPane);
		
		JSplitPane splitPane = new JSplitPane();
		splitPane.setOrientation(JSplitPane.VERTICAL_SPLIT);
		GridBagConstraints gbc_splitPane = new GridBagConstraints();
		gbc_splitPane.fill = GridBagConstraints.BOTH;
		gbc_splitPane.gridx = 0;
		gbc_splitPane.gridy = 0;
		contentPane.add(splitPane, gbc_splitPane);
		
		JPanel upperPanel = new JPanel();
		splitPane.setLeftComponent(upperPanel);
		GridBagLayout gbl_upperPanel = new GridBagLayout();
		gbl_upperPanel.columnWidths = new int[]{0, 0, 0, 0};
		gbl_upperPanel.rowHeights = new int[]{0, 0, 0, 0, 0, 0};
		gbl_upperPanel.columnWeights = new double[]{0.0, 1.0, 0.0, Double.MIN_VALUE};
		gbl_upperPanel.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
		upperPanel.setLayout(gbl_upperPanel);
		
		JLabel lblAnnotationFileLocation = new JLabel("Annotation File Location :");
		GridBagConstraints gbc_lblAnnotationFileLocation = new GridBagConstraints();
		gbc_lblAnnotationFileLocation.insets = new Insets(0, 0, 5, 5);
		gbc_lblAnnotationFileLocation.anchor = GridBagConstraints.EAST;
		gbc_lblAnnotationFileLocation.gridx = 0;
		gbc_lblAnnotationFileLocation.gridy = 0;
		upperPanel.add(lblAnnotationFileLocation, gbc_lblAnnotationFileLocation);
		
		tbannfile = new JTextField();
		GridBagConstraints gbc_tbannfile = new GridBagConstraints();
		gbc_tbannfile.insets = new Insets(0, 0, 5, 5);
		gbc_tbannfile.fill = GridBagConstraints.HORIZONTAL;
		gbc_tbannfile.gridx = 1;
		gbc_tbannfile.gridy = 0;
		upperPanel.add(tbannfile, gbc_tbannfile);
		tbannfile.setColumns(10);
		
		JButton btnBrowse = new JButton("Browse");
		btnBrowse.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				JFileChooser fc = new JFileChooser(".");
				fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
				
				int returnVal = fc.showSaveDialog(XMLGenerateGui.this);
				if(returnVal == JFileChooser.APPROVE_OPTION){
					
					inputFile = fc.getSelectedFile();
					tbannfile.setText(inputFile.getPath());
				}
			}
		});
		GridBagConstraints gbc_btnBrowse = new GridBagConstraints();
		gbc_btnBrowse.insets = new Insets(0, 0, 5, 0);
		gbc_btnBrowse.gridx = 2;
		gbc_btnBrowse.gridy = 0;
		upperPanel.add(btnBrowse, gbc_btnBrowse);
		
		JLabel lblTextFileLocation = new JLabel("Text File Location :");
		GridBagConstraints gbc_lblTextFileLocation = new GridBagConstraints();
		gbc_lblTextFileLocation.anchor = GridBagConstraints.EAST;
		gbc_lblTextFileLocation.insets = new Insets(0, 0, 5, 5);
		gbc_lblTextFileLocation.gridx = 0;
		gbc_lblTextFileLocation.gridy = 1;
		upperPanel.add(lblTextFileLocation, gbc_lblTextFileLocation);
		
		tbtextfile = new JTextField();
		GridBagConstraints gbc_tbtextfile = new GridBagConstraints();
		gbc_tbtextfile.insets = new Insets(0, 0, 5, 5);
		gbc_tbtextfile.fill = GridBagConstraints.HORIZONTAL;
		gbc_tbtextfile.gridx = 1;
		gbc_tbtextfile.gridy = 1;
		upperPanel.add(tbtextfile, gbc_tbtextfile);
		tbtextfile.setColumns(10);
		
		JButton btnBrowse_2 = new JButton("Browse");
		btnBrowse_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JFileChooser fc = new JFileChooser(".");
				fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
				
				int returnVal = fc.showSaveDialog(XMLGenerateGui.this);
				if(returnVal == JFileChooser.APPROVE_OPTION){
					
					inputTextFile = fc.getSelectedFile();
					tbtextfile.setText(inputTextFile.getPath());
				}
			}
		});
		GridBagConstraints gbc_btnBrowse_2 = new GridBagConstraints();
		gbc_btnBrowse_2.insets = new Insets(0, 0, 5, 0);
		gbc_btnBrowse_2.gridx = 2;
		gbc_btnBrowse_2.gridy = 1;
		upperPanel.add(btnBrowse_2, gbc_btnBrowse_2);
		
		JLabel lblTypeOfFile = new JLabel("Type of File :");
		GridBagConstraints gbc_lblTypeOfFile = new GridBagConstraints();
		gbc_lblTypeOfFile.anchor = GridBagConstraints.EAST;
		gbc_lblTypeOfFile.insets = new Insets(0, 0, 5, 5);
		gbc_lblTypeOfFile.gridx = 0;
		gbc_lblTypeOfFile.gridy = 2;
		upperPanel.add(lblTypeOfFile, gbc_lblTypeOfFile);
		
		comboBox = new JComboBox();
		GridBagConstraints gbc_comboBox = new GridBagConstraints();
		gbc_comboBox.insets = new Insets(0, 0, 5, 5);
		gbc_comboBox.fill = GridBagConstraints.HORIZONTAL;
		gbc_comboBox.gridx = 1;
		gbc_comboBox.gridy = 2;
		comboBox.addItem("Daily Strength File");
		comboBox.addItem("Twitter File");
		upperPanel.add(comboBox, gbc_comboBox);
		
		JLabel lblOutputFilesLocation = new JLabel("Output Files Location");
		GridBagConstraints gbc_lblOutputFilesLocation = new GridBagConstraints();
		gbc_lblOutputFilesLocation.anchor = GridBagConstraints.EAST;
		gbc_lblOutputFilesLocation.insets = new Insets(0, 0, 5, 5);
		gbc_lblOutputFilesLocation.gridx = 0;
		gbc_lblOutputFilesLocation.gridy = 3;
		upperPanel.add(lblOutputFilesLocation, gbc_lblOutputFilesLocation);
		
		tboutputfile = new JTextField();
		GridBagConstraints gbc_tboutputfile = new GridBagConstraints();
		gbc_tboutputfile.insets = new Insets(0, 0, 5, 5);
		gbc_tboutputfile.fill = GridBagConstraints.HORIZONTAL;
		gbc_tboutputfile.gridx = 1;
		gbc_tboutputfile.gridy = 3;
		upperPanel.add(tboutputfile, gbc_tboutputfile);
		tboutputfile.setColumns(10);
		
		JButton btnBrowse_1 = new JButton("Browse");
		btnBrowse_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				JFileChooser fc = new JFileChooser(".");
				fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
				
				int returnVal = fc.showSaveDialog(XMLGenerateGui.this);
				if(returnVal == JFileChooser.APPROVE_OPTION){
					outputDir = fc.getSelectedFile();
					tboutputfile.setText(outputDir.getPath());
				}
			}
		});
		GridBagConstraints gbc_btnBrowse_1 = new GridBagConstraints();
		gbc_btnBrowse_1.insets = new Insets(0, 0, 5, 0);
		gbc_btnBrowse_1.gridx = 2;
		gbc_btnBrowse_1.gridy = 3;
		upperPanel.add(btnBrowse_1, gbc_btnBrowse_1);
		
		JButton btnGenerateXmlFiles = new JButton("Generate XML Files");
		btnGenerateXmlFiles.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//
				String str =  comboBox.getSelectedItem().toString();
				if(str.equals("Daily Strength File"))
				{
					filetype = "DS";
				}
				else if(str.equals("Twitter File"))
				{
					filetype = "T";
				}
				
				if(inputFile!=null && outputDir!=null && inputTextFile!=null)
				{
					if(filetype.equals("T"))
					{
						XMLGeneration obj = new XMLGeneration();
						String result = obj.CreateXMLFilesforTwitter(inputFile, inputTextFile, outputDir);
						consolePane.setText(consolePane.getText() + "\n" + result);
					}
					else if(filetype.equals("DS")){
						XMLGeneration obj = new XMLGeneration();
						String result = obj.CreateXMLFilesforDS2(inputFile, inputTextFile, outputDir);
						consolePane.setText(consolePane.getText() + "\n" + result);
					}
				}
			}
		});
		GridBagConstraints gbc_btnGenerateXmlFiles = new GridBagConstraints();
		gbc_btnGenerateXmlFiles.gridx = 2;
		gbc_btnGenerateXmlFiles.gridy = 4;
		upperPanel.add(btnGenerateXmlFiles, gbc_btnGenerateXmlFiles);
		
		JPanel lowerPanel = new JPanel();
		splitPane.setRightComponent(lowerPanel);
		GridBagLayout gbl_lowerPanel = new GridBagLayout();
		gbl_lowerPanel.columnWidths = new int[]{0, 0};
		gbl_lowerPanel.rowHeights = new int[]{0, 0, 0};
		gbl_lowerPanel.columnWeights = new double[]{1.0, Double.MIN_VALUE};
		gbl_lowerPanel.rowWeights = new double[]{1.0, 0.0, Double.MIN_VALUE};
		lowerPanel.setLayout(gbl_lowerPanel);
		
		JScrollPane scrollPane = new JScrollPane();
		GridBagConstraints gbc_scrollPane = new GridBagConstraints();
		gbc_scrollPane.insets = new Insets(0, 0, 5, 0);
		gbc_scrollPane.fill = GridBagConstraints.BOTH;
		gbc_scrollPane.gridx = 0;
		gbc_scrollPane.gridy = 0;
		lowerPanel.add(scrollPane, gbc_scrollPane);
		
		consolePane = new JTextPane();
		scrollPane.setViewportView(consolePane);
		
		JLabel lblNewLabel = new JLabel("Output Result:");
		scrollPane.setColumnHeaderView(lblNewLabel);
		//lowerPanel.setFocusTraversalPolicy(new FocusTraversalOnArray(new Component[]{lblNewLabel, scrollPane, consolePane}));
	}

}
